package 寒假刷题;

import java.util.Random;

//随机数索引
public class Solution {
    int[] nums;
    Random random = new Random();
    public Solution(int[] nums) {
        this.nums = nums;
    }

    public int pick(int target) {
        int ans = 0;
        for (int i = 0,cnt = 0; i < nums.length; i++) {
            if (nums[i]==target){
                cnt++;
                if (random.nextInt(cnt)==0)
                    ans = i;
            }
        }
        return ans;
    }
}
